﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MarioMapEditor
{
    public partial class mainFrm : Form
    {
        public mainFrm()
        {
            InitializeComponent();
        }

        private void cleanAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._mapCleanAll();
        }

        private void _mapCleanAll()
        {
            this.map.Controls.Clear();
            this._buildMapGird();
        }

        private void _buildMapGird()
        {
            // Create line
            Panel line;
            int objSize = PicBox.DIS * PicBox.ZOOM;

            int width = map.AutoScrollMinSize.Width;
            int height = map.AutoScrollMinSize.Height;

            int rows = height / objSize;
            int cols = width / objSize;

            // Draw rows
            for (int i = 0; i <= rows; i++) 
            {
                line = new Panel();
                line.BackColor = System.Drawing.SystemColors.ActiveCaption;
                line.Size = new Size(width, 1);
                line.Location = new Point(0, (i*objSize));

                this.map.Controls.Add(line);
            }

            // Draw cols
            for (int i = 0; i <= cols; i++)
            {
                line = new Panel();
                line.BackColor = System.Drawing.SystemColors.ActiveCaption;
                line.Size = new Size(1, height);
                line.Location = new Point((i * objSize), 0);
                this.map.Controls.Add(line);
            }
        }

        private void _resizeMap(int w, int h)
        {
            this._mapCleanAll();

            this.map.AutoScrollMinSize = new Size(w * PicBox.ZOOM, h * PicBox.ZOOM);

            this._buildMapGird();
        }

        private void x256ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this._resizeMap(512, 256);
        }

        private void x256ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this._resizeMap(1024, 256);
        }

        private void x256ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            this._resizeMap(2048, 256);
        }

        private void x256ToolStripMenuItem3_Click(object sender, EventArgs e)
        {
            this._resizeMap(4096, 256);
        }

        private void map_MouseClick(object sender, MouseEventArgs e)
        {
            int lX, lY, pX, pY, objSize, mod;
            objSize = PicBox.DIS * PicBox.ZOOM;

            if (map.AutoScrollPosition.X < 0)
            {
                lX = e.X - map.AutoScrollPosition.X;
                mod = lX % objSize;
                pX = lX - mod;
                lX = pX + map.AutoScrollPosition.X;
            }
            else
            {
                lX = e.X;
                mod = lX % objSize;
                lX -= mod;
                pX = lX;
            }

            if (map.AutoScrollPosition.X < 0)
            {
                lY = e.Y - map.AutoScrollPosition.Y;
                mod = lY % objSize;
                pY= lY - mod;
                lY = pY + map.AutoScrollPosition.Y;
            }
            else
            {
                lY = e.Y;
                mod = lY % objSize;
                lY -= mod;
                pY = lY;
            }

            PicBox box = new PicBox(1, lX, lY);
            box.setPostion(pX, pY);
            this.map.Controls.Add(box.createPic());
        }

        private void mainFrm_Load(object sender, EventArgs e)
        {
            this._resizeMap(512, 256);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDial = new OpenFileDialog();
            openDial.Filter = "txt files (*.txt)|*.txt";
            openDial.RestoreDirectory = true;

            if (openDial.ShowDialog() == DialogResult.OK)
            {

            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDail = new SaveFileDialog();
            saveDail.Filter = "txt files (*.txt)|*.txt";
            saveDail.RestoreDirectory = true;
            if (saveDail.ShowDialog() == DialogResult.OK)
            {

            }
        }
    }
}
